<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/common.jsp"%>
<div class="am-cf am-padding am-padding-bottom-0">
	<div class="am-fl am-cf">
		<strong class="am-text-primary am-text-lg">个人资料</strong> / <small>Personal
			information</small>
	</div>
</div>

<div class="am-g am-padding">
	<div class="am-u-sm-12 am-u-md-8">
		<form class="am-form am-form-horizontal" id="detailForm">
			<div class="am-form-group">
				<label for="loginName" class="am-u-sm-3 am-form-label">登录账户 /
					Login Name</label>
				<div class="am-u-sm-9 am-form-group am-margin-0">
					<input type="text" id="loginName" name="loginName" placeholder="请输入你的登录账户 / Login Name" value="${user.loginName }"
						required data-validation-message="登录账户不能为空" />
				</div>
			</div>

			<div class="am-form-group">
				<label for="userName" class="am-u-sm-3 am-form-label">用户名 /
					User Name</label>
				<div class="am-u-sm-9 am-form-group am-margin-0">
					<input type="text" id="userName" name="userName" placeholder="请输入你的用户名 / User Name" value="${user.userName }"
						required data-validation-message="用户名不能为空" />
				</div>
			</div>

			<div class="am-form-group">
				<label for="loginPwd" class="am-u-sm-3 am-form-label">密码 /
					Login Name</label>
				<div class="am-u-sm-9 am-form-group am-margin-0">
					<input type="password" id="loginPwd" name="loginPwd" placeholder="请输入你的密码 / Login Password" />
				</div>
			</div>

			<div class="am-form-group">
				<label for="loginPwdEnsure" class="am-u-sm-3 am-form-label">确认密码 /
					Login Name</label>
				<div class="am-u-sm-9 am-form-group am-margin-0">
					<input type="password" id="loginPwdEnsure" placeholder="请确认您的密码 / Ensure Your Login Password"
						data-equal-to="#loginPwd" />
				</div>
			</div>
			
			<div class="am-margin" style="text-align: center;">
		      <button type="submit" class="am-btn am-btn-primary am-btn-xs">提交保存</button>
		      <button type="button" class="am-btn am-btn-primary am-btn-xs" onclick="exitShow()">放弃保存</button>
		    </div>
			
			<input type="hidden" name="seqId" id="seqId" value="${user.seqId }">
		</form>
	</div>
	
	<script>
	$(function(){
		var $form = $('#detailForm');
		
		var user = {
			save : function(seqId){
				// 更新
				if(seqId) {
					$.ajax({url : "${basePath}/manage/user/modify/" + seqId,
						data : $("#detailForm").serialize(),
						dataType : "html",
						method : "post",
						success : function(data) {
							var result = user.dealSaveCallback(data);
							if(result == true){
								layer.msg("更新成功！",{icon: 1});
								exitShow();
							}
						},
						error : function(){
							layer.msg("服务器出错，更新失败！",{icon: 2});
						}
					});
				}
				// 新增
				else {
					$.ajax({url : "${basePath}/manage/user/save",
						data : $("#detailForm").serialize(),
						dataType : "html",
						method : "post",
						success : function(data) {
							var result = user.dealSaveCallback(data);
							if(result == true){
								layer.msg("更新失败！",{icon: 2});
								exitShow();
							}
						},
						error : function(){
							layer.msg("服务器出错，更新失败！",{icon: 2});
						}
					});
				}
			},
			dealSaveCallback : function(data){
				if(data == "DM_ER"){
					layer.msg("更新用户时遇到错误！",{icon: 2});
				}
				else if(data == "UN_NP"){
					layer.msg("用户名不能为空！",{icon: 2});
				}
				else if(data == "LN_NP"){
					layer.msg("登录名不能为空！",{icon: 2});
				}
				else if(data == "LP_NP"){
					layer.msg("登录密码不能为空！",{icon: 2});
				}
				else if(data == "LP_NP"){
					throw "修改时传入的主键为空";
					layer.msg("更新失败！",{icon: 2});
				}
				else {
					return true;
				}
			}
		}
		
		$form.validator({
			onValid : function(validity) {
				$(validity.field).closest('.am-form-group').find('.am-alert').hide();
			},
			onInValid : function(validity) {
				var $field = $(validity.field);
				var $group = $field.closest('.am-form-group');
				var $alert = $group.find('.am-alert');
				// 使用自定义的提示信息 或 插件内置的提示信息
				var msg = $field.data('validationMessage') || this.getValidationMessage(validity);
				
				if (!$alert.length) {
					$alert = $('<div class="am-alert am-alert-danger"></div>').hide().appendTo($group);
				}
				
				$alert.html(msg).show();
			},
			submit : function() {
				var formValidity = this.isFormValid();
				
				// 表单验证未成功
				if (!formValidity) {
					console.warn('验证状态：', '未通过');
				} else{
					user.save($("#seqId").val());
			   	}
				return false;
			}
		});
	});
	</script>
</div>